let RouterPlugin = function(){
	this.$router = null;
	this.$store = null;
};

RouterPlugin.install = function (vue,router,store,i18n){
	this.$router = router;
	this.$store = store;
	this.$vue = new vue({ i18n });
	function isURL (s){
		return /^http[s]?:\/\/.*/.test(s);
	}

	function objToform (obj){
		let result = [];
		Object.key(obj).forEach(key => {
			result.push(`${key}=${obj[key]}`);
		})
		return result.join('&');
	}

	this.$router.$ipcRouter = {
		$website:this.$store.getters.website,
		routerList:[],
		group:'',
		meta: {},
		safe:this,
		//设置标题
		setTitle: (title) => {
			const defaultTitle = this.$vue.$t('title');
			title = title ? `${title}--${defaultTitle}` : defaultTitle;
			document.title = title;
		},
		//关闭标签
		generateTitle: (title,key) => {
			console.log("%cgenerateTitle","background:#600;color:#fff")
			console.log(title)
			console.log(key)

			if( !key ) return title;
			const hasKey = this.$vue.$te('route.' + key);

			if(hasKey){
				const translatedTitle = this.$vue.$t('route.' + key);
				return translatedTitle
			}

			return title
		},
		//处理路由
		getPath: function (params) {
			let { src } = params;
			let result = src || '/';
			if(src.includes("http") || src.includes("https")) {
				result = `/myiframe/urlPath?${objToform(params)}`
			}
			return result;
		},
		//正则处理路由
		validPath: function (list,path){
			let result = false;
			list.forEach(item => {
				if(new RegExp("^" + item + ".*","g").test(path)) {
					result = true
				}
			})

			return result;
		},
		//设置路由值
		getValue: function(route){
			let value = "";
			if(route.query.src) {
				value = route.query.src;
			} else {
				value = route.path;
			}
			return value;
		},
		//动态路由
		formatRoutes: function (aMenu = [], first){

			console.log("-------动态路由-formatRoutes-----------")
			console.log(aMenu)

			const aRouter = [];

			//console.log("---------this.$website-------------")
			//console.log(this.$website)

			const propsConfig = this.$website.menu.props;
			const iconDefault = this.$website.menu.iconDefault;

			const propsDefault = {
				label: propsConfig.label || 'label',
				path: propsConfig.path || 'path',
				icon: propsConfig.icon || 'icon',
				children: propsConfig.children || 'children',
				meta: propsConfig.meta || 'meta'
			}

			if(aMenu.length === 0) return;
			for(let i=0; i < aMenu.length; i++){
				const oMenu = aMenu[i];

				if(this.routerList.includes(oMenu[propsDefault.path])) return;

				let path = (() => {
					if(first) {
						return oMenu[propsDefault.path].replace('/index','');
					} else {
						return oMenu[propsDefault.path]
					}
				})(),
				component = 'views' + oMenu[propsConfig.path],
				name = oMenu[propsDefault.label],
				icon = oMenu[propsDefault.icon] || iconDefault,
				children = oMenu[propsDefault.children],
				meta = oMenu[propsDefault.meta] || {};

				meta = Object.assign(meta,(function (){
					var _meta = {};
          			if (meta.keepAlive === true) {
            			_meta['$keepAlive'] = true;
          			}

          			if (oMenu['ismenu'] && oMenu['ismenu'] === 'Y'){
            			_meta['isTab'] = true;
          			}else{
            			_meta['isTab'] = false;
          			}

          			return _meta;

				})());

				const isChild = children.length !== 0;

				const oRouter = {
					path: path,
					component (resolve) {
						if(first) {
							require(['../layout/index'],resolve)
							return
						} else if (isChild && !first) {
							require(['../layout/index'],resolve)
							return
						} else {


							
							
							require([`../${component}.vue`], resolve)
						}
					},
					name: name,
					icon: icon,
					meta: meta,
					redirect: (() => {
						if (!isChild && first && !isURL(path)) return `${path}/index`
						else return '';
					})(),
					children: !isChild ? (() => {
						if(first){
							if(!isURL(path)) oMenu[propsDefault.path] = `${path}/index`;
							return [{
								component (resolve) { require([`../${component}.vue`], resolve) },
								icon: icon,
								name: name,
								meta: meta,
								path: 'index'
							}]
						}
						return [];
					})() : (() => {
						return this.formatRoutes(children,false)
					})()
				}
				aRouter.push(oRouter)
			}  

			console.log("-----------aRouter-----------")
			console.log(aRouter)


			if(first) {
				if(!this.routerList.includes(aRouter[0][propsDefault.path])){
					this.safe.$router.addRoutes(aRouter)
					this.routerList.push(aRouter[0][propsDefault.path])
				}
			} else {
				return aRouter
			}

		}
		
	}
}

export default RouterPlugin;